export default class Button extends Phaser.GameObjects.Container{

	constructor (scene, x = 0, y = 0 , title = '', func, texture = 'comm/button.png')
	{
		super(scene,x,y)
		this.sprite = scene.add.sprite(x, y,texture).setInteractive()
		this.sprite.on(Phaser.Input.Events.POINTER_DOWN, func)
		if(title){
			let w = this.byteLength(title) * 12 / 2
			this.text = scene.add.text(x - w, y - 10,title,{fontSize:'24px',stroke:'#000000',strokeThickness:3})
		}
	}

	setVisible(op = true) {
		this.sprite.setVisible(op)
		if(this.text){
			this.text.setVisible(op)
		}
	}

	byteLength(str) {
		let l = 0
		if(str) {
			for(var i = 0; i < str.length; i ++) {
				if(str.charCodeAt(i) > 255) {
					l += 2
				}else {
					l ++
				}
			}
			return l
		} else {
			return 0
		}
	}
}